using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CXMLStudioIDE.WinFormsUI.CXMLStudioControls;
using CXMLStudioIDE.Common;
using System.Collections;
using CXMLStudioIDE.Common.Entities;
using System.Diagnostics;
using System.Reflection;

namespace CXMLStudioIDE.Forms
{
    public partial class TreeViewWindow : ToolWindowForm
    {
        public CXMLStudioIDE.WinFormsUI.CXMLStudioControls.CXMLTreeView cxmlTreeView1;
        public delegate void DragDropFinishedHandler(CXMLTreeNode sender, DragDropEffects e);
        public event DragDropFinishedHandler DragDropFinished;
        private MainForm m_parent;
        private int cont = 0;
        private List<CXMLTreeView> m_treeviews;

        public delegate void OnTreeViewChangedEventHandler(object sender);
        public event OnTreeViewChangedEventHandler OnTreeViewChanged;

        public TreeViewWindow(Form parent)
        {
            m_parent = (MainForm)parent;
            InitializeComponent();
            m_treeviews = new List<CXMLTreeView>();
            OnTreeViewChanged += new OnTreeViewChangedEventHandler(OnTreeViewChangedAction);
        }

        public void StartProgress()
        {
            timer1.Start();
            pnlProgress.Visible = true;
            pnlProgress.Refresh();
        }
        public void StopProgress()
        {
            timer1.Stop();
            progressBar1.Value = 0;
            pnlProgress.Visible = false;
        }
        public int InitCXMLTreeView(DisplayContext context)
        {
            // 
            // cxmlTreeView1 Create new instance of CXMLTreeView and
            // add it to m_treeviewlist
            // 
            ++cont;
            RemoveTreeView();
            this.cxmlTreeView1 = new CXMLStudioIDE.WinFormsUI.CXMLStudioControls.CXMLTreeView();
            this.SuspendLayout();
            this.cxmlTreeView1.BaseColor = System.Drawing.Color.Empty;
            this.cxmlTreeView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.cxmlTreeView1.ImportToDB = false;
            this.cxmlTreeView1.Location = new System.Drawing.Point(0, 3);
            this.cxmlTreeView1.Name = "cxmlTreeView1";
            this.cxmlTreeView1.DisplayContext = context;
            this.cxmlTreeView1.ObjectName = "";
            this.cxmlTreeView1.RootNodeName = "";
            this.cxmlTreeView1.SelectedPropertySet = null;
            this.cxmlTreeView1.Size = new System.Drawing.Size(208, 402);
            this.cxmlTreeView1.TabIndex = 0;
            this.cxmlTreeView1.NodeMouseClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.cxmlTreeView1_NodeMouseClick);
            this.cxmlTreeView1.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.cxmlTreeView1_ItemDrag);
            this.cxmlTreeView1.TreeViewId = cont;
            this.Controls.Add(this.cxmlTreeView1);
            m_treeviews.Add(this.cxmlTreeView1);
            this.ResumeLayout();
            OnTreeViewChanged(this);
            //IntPtr handle = this.cxmlTreeView1.Handle;
            return this.cxmlTreeView1.TreeViewId;
        }
        public int GetTreeViewId(BusinessEntity be, DisplayContext context)
        {
            foreach (CXMLTreeView tv in m_treeviews)
            {
                if (tv != null && tv.DisplayContext == context && tv.Nodes.Count > 0
                    && ((CXMLTreeNode)tv.Nodes[0]).BusinessEntity.Equals(be))
                {
                    return tv.TreeViewId;
                }
            }
            return -1;
        }

        /** Gets the TreeView from list corresponding to the ID passed
        *  then removes the Treeview from Form.Controls and adds
        *  the new treeview to the form */
        public void SetActiveTreeView(int TreeViewId)
        {
            foreach (CXMLTreeView tv in m_treeviews)
            {
                if (tv != null && tv.TreeViewId.Equals(TreeViewId))
                {
                    RemoveTreeView();
                    this.cxmlTreeView1 = null;
                    this.cxmlTreeView1 = tv;
                    this.cxmlTreeView1.Refresh();
                    this.Controls.Add(this.cxmlTreeView1);
                    this.Refresh();
                    OnTreeViewChanged(this);
                }
            }
        }

        private void RemoveTreeView()
        {
            CXMLTreeView toremovetv = this.Controls["cxmlTreeView1"] as CXMLTreeView;
            if (toremovetv != null)
            {
                this.Controls.Remove(toremovetv);
            }
        }
        private void OnTreeViewChangedAction(object sender)
        {
            if (this.cxmlTreeView1 != null)
            {
                SetContextualMenu(this.cxmlTreeView1.DisplayContext);
            }
        }
        private void SetContextualMenu(DisplayContext context)
        {
            switch (context)
            {
                case DisplayContext.Deafult:
                case DisplayContext.FormDesigner:
                    foreach (ToolStripItem item in this.contextMenuStrip1.Items)
                    {
                        item.Visible = (item.Name.StartsWith("properties") ? true : false);
                    }
                    break;
                case DisplayContext.GoldCXML:
                    foreach (ToolStripItem item in this.contextMenuStrip1.Items)
                    {
                        item.Visible = (item.Name.StartsWith("properties") ||
                                item.Name.StartsWith("AddChild") ? true : false);
                    }
                    foreach (ToolStripItem item in this.AddChildToolStripMenuItem.DropDownItems)
                    {
                        item.Visible = (item.Name.StartsWith("instance") ? true : false);
                    }
                    break;
                default:
                    foreach (ToolStripItem item in this.contextMenuStrip1.Items)
                    {
                        item.Visible = true;
                    }
                    break;
            }
        }
        private void cxmlTreeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Text.ToString() != null)
            {
                if (e.Button == MouseButtons.Right)
                {
                    cxmlTreeView1.SelectedNode = cxmlTreeView1.GetNodeAt(e.X, e.Y);
                    contextMenuStrip1.Show(cxmlTreeView1, e.X, e.Y);
                }
                if (e.Button == MouseButtons.Left)
                {
                    this.cxmlTreeView1.SelectedNode = this.cxmlTreeView1.GetNodeAt(e.X, e.Y);
                }
            }

        }

        private void elementToolStripMenuItem_Click(object sender, EventArgs e)
        {
            cxmlTreeView1.SelectedNode.Nodes.Insert(0, "New Element");
            cxmlTreeView1.SelectedNode.Expand();
        }

        private void elementToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            cxmlTreeView1.SelectedNode.Parent.Nodes.Insert(cxmlTreeView1.SelectedNode.Index, "New Element");
        }

        public DragDropEffects DoDragDropItem(CXMLTreeNode node, DragDropEffects allowedEffects)
        {
            DragDropEffects e = base.DoDragDrop(node, allowedEffects);
            if (this.DragDropFinished != null)
            {
                this.DragDropFinished(node, e);
            }
            return e;
        }

        private void cxmlTreeView1_ItemDrag(object sender, ItemDragEventArgs e)
        {
            CXMLTreeNode node = e.Item as CXMLTreeNode;
            if (node != null)
            {
                DragNode(node, 10, 10);
            }
        }

        private void DragNode(CXMLTreeNode node, int x, int y)
        {
            node.X = x;
            node.Y = y;
            DragDropEffects none = DragDropEffects.None;
            none = DoDragDropItem(node, DragDropEffects.Copy);
        }

        private void propertiesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_parent.PropertyGridSelectedObject = cxmlTreeView1.SelectedNode;
            m_parent.ShowPropertyWindow();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //progressBar1.Increment(1);
            progressBar1.PerformStep();
            if (progressBar1.Value == progressBar1.Maximum)
            {
                progressBar1.Value = 0;
            }
        }
        private void instanceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Assembly assembly = Assembly.LoadFrom("PartyType.dll");
            //Type placestype = assembly.GetType("Enterprise.PlacesType");
            CXMLTreeNode node = this.cxmlTreeView1.SelectedNode as CXMLTreeNode;
            //Type placestype = Common.Common.GetTypeFromString("Enterprise.PlacesType");
            //this.cxmlTreeView1.PrepareForInstances(null, node.BusinessEntity, node);
            if (node != null)
            {
                m_parent.NewAddInstanceForm(node);
            }
        }
    }
}